package servlet;

import service.MeetingRoomService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteRoom")
public class DeleteMeetingRoomServlet extends HttpServlet {
    private MeetingRoomService roomService = new MeetingRoomService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取删除的id
        Integer id = Integer.parseInt(req.getParameter("id"));

        // 2. 调用业务层删除
        boolean isSuccess = roomService.deleteRoom(id);

        // 3. 存储删除结果提示
        if (isSuccess) {
            req.setAttribute("msg", "删除成功");
        } else {
            req.setAttribute("msg", "删除失败");
        }

        // 4. 重定向到查询全部（避免表单重复提交）
        resp.sendRedirect(req.getContextPath() + "/listRooms");
    }
}